﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

using E.Model;
using X.DALSqlServer;

namespace E.DAL
{
    public class FeedBackDAL
    {
        /// <summary>
        /// 添加反馈
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int AddFeedBack(FeedBackModel model)
        {
            IDataParameter[] parms = new IDataParameter[] { 
                new SqlParameter("@UserName", model.UserName),
                new SqlParameter("@Title", model.Title),
                new SqlParameter("@Content", model.Content),
                new SqlParameter("@IP", model.IP),
                new SqlParameter("@AddDate", model.AddDate),

            };
            string sql = "INSERT INTO E_Feedback(UserName,Title,Content,IP,AddDate)VALUES(@UserName,@Title,@Content,@IP,@AddDate)";
            return SqlHelper.ExecuteNonQueryBySql(sql, parms);
        }

        /// <summary>
        /// 获取反馈列表
        /// </summary>
        /// <param name="searchModel"></param>
        /// <param name="pageSize"></param>
        /// <param name="pageIndex"></param>
        /// <returns></returns>
        public FeedBackListModel GetFeedBackListByCondition(FeedBackModel searchModel, int pageSize, int pageIndex)
        {
            FeedBackListModel listModel = new FeedBackListModel();
            IDataParameter[] parms = new IDataParameter[] { 
                new SqlParameter("@UserName", searchModel.UserName),
                new SqlParameter("@Title", searchModel.Title),
                new SqlParameter("@FromDate", searchModel.FromDate),
                new SqlParameter("@ToDate", searchModel.ToDate),
                new SqlParameter("@PageSize", pageSize),
                new SqlParameter("@PageIndex", pageIndex)
            };
            IList<FeedBackModel> list = new List<FeedBackModel>();
            using (IDataReader dr = SqlHelper.ExecuteDataReaderByStoredProc("sp_getFeedBackListByCondition", parms))
            {
                int totalCount = 0;
                while (dr.Read())
                {
                    if (dr["ID"] == DBNull.Value)
                    {
                        totalCount = Convert.ToInt32(dr["pos"]);
                    }
                    else
                    {
                        FeedBackModel model = new FeedBackModel();
                        model.ID = (int)dr["ID"];
                        model.UserName = dr["UserName"].ToString();
                        model.Title = dr["Title"].ToString();
                        model.AddDate = (DateTime)dr["AddDate"];
                        list.Add(model);
                    }
                }
                listModel.TotalCount = totalCount;
                listModel.List = list;
                return listModel;
            }
        }

        /// <summary>
        /// 获取反馈信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public FeedBackModel GetFeedBackInfo(int id)
        {
            string sql = "SELECT ID,UserName,Title,Content,IP,AddDate FROM E_Feedback WHERE ID = " + id;
            using (IDataReader dr = SqlHelper.ExecuteDataReaderBySql(sql))
            {
                FeedBackModel model = new FeedBackModel();
                if (dr.Read())
                {
                    model.ID = (int)dr["ID"];
                    model.UserName = dr["UserName"].ToString();
                    model.Title = dr["Title"].ToString();
                    model.Content = dr["Content"].ToString();
                    model.IP = dr["IP"].ToString();
                    model.AddDate = (DateTime)dr["AddDate"];
                }
                else
                    model.ID = -1;
                return model;
            }
        }

        /// <summary>
        /// 删除反馈
        /// </summary>
        /// <param name="feedbackid"></param>
        /// <returns></returns>
        public int DelFeedBack(string[] feedbackid)
        {
            string sql = "DELETE FROM E_Feedback WHERE " + DALUtils.GetInCondition("ID", feedbackid);
            return SqlHelper.ExecuteNonQueryBySql(sql);
        }


    }
}
